package com.application.lucky.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.application.lucky.R;
import com.application.lucky.utils.CommonUtils;


/**
 * Created by lixuce on 2018/3/6.
 */

public abstract class BaseBottomEnterDialog extends Dialog {

    protected View rootView;

    public BaseBottomEnterDialog(@NonNull Context context) {
        super(context, R.style.Dialog_audio_room);
        rootView = LayoutInflater.from(context).inflate(layoutResId(), null, false);
        setContentView(rootView);
        setCanceledOnTouchOutside(true);
        getWindow().getAttributes().gravity = Gravity.BOTTOM;
        getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().getAttributes().height = WindowManager.LayoutParams.WRAP_CONTENT;
        initViews(context);
    }

    public BaseBottomEnterDialog(@NonNull Context context, int dialog) {
        super(context, dialog);
        rootView = LayoutInflater.from(context).inflate(layoutResId(), null, false);
        setContentView(rootView);
        setCanceledOnTouchOutside(true);
        getWindow().getAttributes().gravity = Gravity.BOTTOM;
        getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().getAttributes().height = WindowManager.LayoutParams.WRAP_CONTENT;
        initViews(context);
    }

    boolean isHideVirtualKey = false;
    // 可设置是否全屏下的效果
    public BaseBottomEnterDialog(@NonNull Context context, boolean isHideVirtualKey) {
        super(context, R.style.Dialog_audio_room);

        this.isHideVirtualKey = isHideVirtualKey;
        if(this.isHideVirtualKey) {

            CommonUtils.setHideVirtualKey(getWindow());
            getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    CommonUtils.setHideVirtualKey(getWindow());
                }
            });
        }

        rootView = LayoutInflater.from(context).inflate(layoutResId(), null, false);
        setContentView(rootView);
        setCanceledOnTouchOutside(true);
        getWindow().getAttributes().gravity = Gravity.BOTTOM;
        getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().getAttributes().height = WindowManager.LayoutParams.WRAP_CONTENT;
        initViews(context);
    }
    public BaseBottomEnterDialog(@NonNull Context context, boolean isHideVirtualKey, int dialog) {
        super(context,dialog);

        this.isHideVirtualKey = isHideVirtualKey;
        if(this.isHideVirtualKey) {

            CommonUtils.setHideVirtualKey(getWindow());
            getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    CommonUtils.setHideVirtualKey(getWindow());
                }
            });
        }

        rootView = LayoutInflater.from(context).inflate(layoutResId(), null, false);
        setContentView(rootView);
        setCanceledOnTouchOutside(true);
        getWindow().getAttributes().gravity = Gravity.BOTTOM;
        getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().getAttributes().height = WindowManager.LayoutParams.WRAP_CONTENT;
        initViews(context);
    }

    protected abstract void initViews(Context context);

    protected abstract int layoutResId();

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(this.isHideVirtualKey) {
            CommonUtils.setHideVirtualKey(getWindow());
        }
    }
}
